package com.gali.juc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Auther: gali
 * @Date: 2022-08-22 15:10
 * @Description:
 */
public class LockSupportDemo {
    public static void main(String[] args) {
//        waitAndNotify();
//        lockAndCondition();
        Thread a = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "\t come in ");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "\t 被唤醒 ");
        }, "a");
        a.start();
//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        new Thread(()->{
            //给某个线程发通行证
            LockSupport.unpark(a);
            System.out.println(Thread.currentThread().getName()+"\t 发出通知 ");
        },"b").start();
    }
    private static void lockAndCondition() {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        new Thread(()->{
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"\t come in ");
                condition.await();
                System.out.println(Thread.currentThread().getName()+"\t 被唤醒 ");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"a").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName()+"\t 发出通知 ");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"b").start();
    }

    private static void waitAndNotify() {
        Object object = new Object();
        new Thread(()->{
            synchronized (object){
                System.out.println(Thread.currentThread().getName()+"\t come in");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"\t 被唤醒");
            }
        },"a").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
            synchronized (object){
                object.notify();
                System.out.println(Thread.currentThread().getName()+"\t 发出通知");
            }
        },"b").start();
    }
}
